<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class Resource_c extends CI_Controller {
        function __construct() {
            parent::__construct();
            $this->load->model(array('files_m', 'event_m', 'checklist_m'));
        }
        
        function index() {
            $data['content'] = 'pages/resource/manage_resource';
            $data['title'] = 'Manage Resource';
            $data['current_page'] = 'manage-resource-page';
            $data['files'] = $this->files_m->get_files();
            $data['venues'] = $this->event_m->get_venues();
            $data['event_types'] = $this->event_m->get_event_types();
            $this->load->view('includes/template', $data);
        }
        
        function files(){
            echo json_encode(array("aaData" => $this->files_m->get_files()));
        }
        
        function delete_file() {
            $ids = $this->input->post('file_id');
            $msg = '';
            $result = $this->files_m->delete_file($ids);
            foreach ($result as $path) {
                unlink('./'.$path->file_path);
            }
            if(sizeof($ids)>1){
                $msg = 'Files successfully deleted.';
            }else{
                $msg = 'File successfully deleted.';
            }
            echo json_encode(array('msg'=>$msg));
        }
        
        function upload_file() {
            $file_element_name = 'userfile';
            $status = '';
            $msg = '';
            $file_data = '';
            if (empty($_POST['filename'])){
               $status = "error";
               $msg = 'Please enter a filename';
               $file_data = '';
            }

            if($status != 'error'){
                $config['upload_path'] = './uploads/';
                $config['file_name'] = $this->input->post('filename');
                $config['allowed_types'] = 'pdf';

                $this->load->library('upload', $config);

                if (!$this->upload->do_upload($file_element_name)){
                   $status = 'error';
                   $msg = $this->upload->display_errors('', '');
                   $file_data = '';
                }else{
                   $data = $this->upload->data();
                   $file = array(
                       'file_name' => $this->input->post('filename'),
                       'file_path' => 'uploads/' . $data['file_name'],
                       'file_status' => '1',
                       'file_type' => $this->input->post('type'),
                       'date_uploaded' => date('Y-m-d'),
                       'coordinator_id' => $this->session->userdata('accid'),
                   );
                   $file_id = $this->files_m->insert_file($file);
                   if($file_id){
                      $status = 'success';
                      $msg = 'File successfully uploaded';
                      $file_data = $this->files_m->get_file($file_id);
                   }else{
                      unlink($data['full_path']);
                      $status = "error";
                      $msg = 'Something went wrong when saving the file, please try again.';
                      $file_data = '';
                   }
                }
                @unlink($_FILES[$file_element_name]);
            }
            echo json_encode(array('status' => $status, 'msg' => $msg, 'file_data' => $file_data));
        }
        
        function get_category() {
            echo json_encode($this->checklist_m->get_all_category());
        }
        
        function get_sub_category($id) {
            echo json_encode($this->checklist_m->get_sub_cats($id));
        }
        
        function save_category() {
            $status = '';
            $cat = '';
            $msg = '';
            $data = array('category_name' => $this->input->post('category'));
            if($this->checklist_m->check_if_cat_exist($this->input->post('category'))=='dont exist'){
                $id = $this->checklist_m->insert_cat($data);
                if($id){
                    $cat = $this->checklist_m->get_category($id);
                    $status = 'success';
                    $msg = 'Category <b>'.$data['category_name'].'</b> successfully saved.';
                }else{
                    $cat = '';
                    $status = 'error';
                    $msg = 'Something went wrong while saving the category. Please try again later.';
                }
            }else{
                $cat = 'already exist';
                $status = 'error';
                $msg = 'Category already exist.';
            }
            echo json_encode(array('status'=>$status, 'cat'=>$cat, 'msg'=>$msg));
        }
        
        function save_sub_category() {
            $status = '';
            $sub_cat = '';
            $msg = '';
            $data = array(
                'list_description' => $this->input->post('sub_category'),
                'category_id' => $this->input->post('cat_id'),
            );
            if($this->checklist_m->check_if_sub_cat_exist($data['list_description'], $data['category_id'])=='dont exist'){
                $id = $this->checklist_m->insert_sub_cat($data);
                if($id){
                    $sub_cat = $this->checklist_m->get_sub_cat($id);
                    $status = 'success';
                    $msg = 'Sub category <b>'.$data['list_description'].'</b> successfully saved.';
                }else{
                    $sub_cat = '';
                    $status = 'error';
                    $msg = 'Something went wrong while saving the category. Please try again later.';
                }
            }else{
                $sub_cat = '';
                $status = 'error';
                $msg = 'Sub category already exist.';
            }
            echo json_encode(array('status'=>$status, 'sub_cat'=>$sub_cat, 'msg'=>$msg));
        }
        
        function delete_sub_category($sub_cat_id) {
            $status = '';
            $msg = '';
            $result = $this->checklist_m->delete_sub_cat($sub_cat_id);
            if($result>0){
                $status = 'success';
                $msg = 'Category successfully deleted.';
            }else{
                $status = 'success';
                $msg = 'Something went wrong while deleting the sub category. Please try again later.';
            }
            echo json_encode(array('status'=>$status, 'msg'=>$msg));
        }
        
        function delete_category($cat_id) {
            $status = '';
            $msg = '';
            $result = $this->checklist_m->delete_cat($cat_id);
            if($result>0){
                $status = 'success';
                $msg = 'Category successfully deleted.';
            }else{
                $status = 'success';
                $msg = 'Something went wrong while deleting the category. Please try again later.';
            }
            echo json_encode(array('status'=>$status, 'msg'=>$msg));
        }
        
        function venues_ajax() {
            echo json_encode(array('aaData' => $this->event_m->get_venues()));
        }
        
        function edit_venue($id) {
            echo json_encode($this->event_m->get_venue($id));
        }
        
        function save_venue() {
            $status = '';
            $msg = '';
            $venue_data = '';
            $data = array(
                'venue_name' => $this->input->post('venue_name'),
                'venue_address' => $this->input->post('venue_address')
            );
            if($this->event_m->check_if_venue_exist($data['venue_name'],$data['venue_address'])<1){
                $id = $this->event_m->insert_venue($data);
                if($id){
                    $venue = $this->event_m->get_venue($id);
                    $status = 'success';
                    $msg = 'Venue <b>'.$venue->venue_name.'</b> successfully saved.';
                    $venue_data = $venue;
                }else{
                    $status = 'error';
                    $msg = 'Something went wrong while saving the venue. Please try again later.';
                    $venue_data = '';
                }
            }else{
                $status = 'error';
                $msg = 'Venue already exist.';
                $venue_data = 'already exist';
            }
            echo json_encode(array('msg' => $msg, 'status' => $status, 'venue'=>$venue_data));
        }
        
        function delete_venue() {
            $ids = $this->input->post('venue_id');
            $res = $this->event_m->delete_venue($ids);
            $status = '';
            $msg = '';
            if($res==null){
                $status = 'success';
                if(sizeof($ids)>1){
                    $msg = 'Venues successfully deleted.';
                }else{
                    $msg = 'Venue successfully deleted.';
                }
            }else if ($res==1451){
                $status = 'error';
                if(sizeof($ids)>1){
                    $msg = 'Oops! Some of the venue your trying to delete are currently in use.';
                }else{
                    $msg = 'Oops! The venue your trying to delete is currently in use.';
                }
            }
            echo json_encode(array('status'=>$status, 'msg'=>$msg));
        }
        
        function event_types_ajax(){
            echo json_encode(array('aaData' => $this->event_m->get_event_types()));
        }
        
        function edit_event_type($id) {
            echo json_encode($this->event_m->get_event_type($id));
        }
        
        function save_event_type() {
            $status = '';
            $msg = '';
            $type_data = '';
            if($this->event_m->check_if_type_exist($this->input->post('event_type_name'))<1){
                $id = $this->event_m->insert_event_type($this->input->post('event_type_name'));
                if($id){
                    $type = $this->event_m->get_event_type($id);
                    $status = 'success';
                    $msg = 'Event type <b>'.$type->type_name.'</b> successfully saved.';
                    $type_data = $type;
                }else{
                    $status = 'error';
                    $msg = 'Something went wrong while saving the event type. Please try again later.';
                    $type_data = '';
                }
            }else{
                $status = 'error';
                $msg = 'Event type already exist.';
                $type_data = 'already exist';
            }
            echo json_encode(array('status' => $status, 'msg' => $msg, 'type'=>$type_data));
        }
        
        function delete_event_type() {
            $ids = $this->input->post('type_id');
            $res = $this->event_m->delete_event_type($ids);
            $status = '';
            $msg = '';
            if($res==null){
                $status = 'success';
                if(sizeof($ids)>1){
                    $msg = 'Event types successfully deleted.';
                }else{
                    $msg = 'Event type successfully deleted.';
                }
            }else if ($res==1451){
                $status = 'error';
                if(sizeof($ids)>1){
                    $msg = 'Oops! Some of the event types your trying to delete are currently in use.';
                }else{
                    $msg = 'Oops! The event type your trying to delete is currently in use.';
                }
            }
            echo json_encode(array('status'=>$status, 'msg'=>$msg));
        }
    }
